package com.noahplat.user.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.Menu;
import com.noahplat.user.service.MenuService;
import com.noahplat.user.utils.MenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 菜单栏相关操作
 *
 * @author
 * @version 1.0.0
 * @Date: 2021/2/27 11:58
 **/

@RequestMapping("/api/menu")
@RestController
@Validated
public class MenuController {


    @Autowired
    private MenuService menuService;

    /**
     * 查询当前用户侧边栏
     *
     * @author
     * @version 1.0.0
     * @Date: 2021/2/27 11:58
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:getMenuList')")
    @GetMapping("/getMenuList")
    public Result getMenuList(@NotNull(message = "参数不能为空") String systype, @Min(value = 1, message = "用户Id不能为空") Long uid) {
        return menuService.getMenuList(systype, uid);
    }

    /**
     * 分页获取菜单
     *
     * @param del
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:getMenus')")
    @GetMapping("/getMenus")
    public Result getMenus(@RequestParam String del, @RequestParam String stid, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        return menuService.getMenus(del, stid, page, limit);
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:addMenu')")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        return menuService.addMenu(menu);
    }

    /**
     * 修改菜单
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:updateMenu')")
    @PutMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu) {
        return menuService.updateMenu(menu);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:delMenu')")
    @DeleteMapping("/delMenu")
    public Result delMenu(@NotNull(message = "ID不能为空 ") @Min(value = 1, message = "Id不能为空") Long id) {
        return menuService.delMenu(id);
    }

    /**
     * 批量添加菜单
     *
     * @param menuDao
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:inserMenu')")
    @PostMapping("/inserMenu")
    public Result inserMenu(MenuDao menuDao) {
        return menuService.inserMenu(menuDao);
    }


    /**
     * 通过系统Id查询父菜单栏
     *
     * @param stid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:MenuController:getParentMenusByStid')")
    @GetMapping("/getParentMenusByStid")
    public Result getParentMenusByStid(@RequestParam String stid, @RequestParam Long acctid) {
        return menuService.getParentMenusByStid(stid, acctid);
    }
}
